package com.study.sfd.person.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.common.utils.Result;
import com.study.sfd.person.dao.DruggistDoctorRelationDao;
import com.study.common.entity.DoctorEntity;
import com.study.sfd.person.entity.DruggistDoctorRelationEntity;
import com.study.sfd.person.enums.VerifyStatus;
import com.study.sfd.person.service.DoctorService;
import com.study.sfd.person.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * doctor(医生)
 *
 * @author 王个球
 * @email wangdajunzy@163.com
 * @date 2021-11-12 13:45:41
 */
@Slf4j
@RestController
@RequestMapping("person/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @Autowired
    private DruggistDoctorRelationDao druggistDoctorRelationDao;

    /**
     * 根据医生 id 获取药商的 ids
     */
    @GetMapping("/ids/{druggistId}")
    public Result<List<Integer>> getIdsByDruggistId(@PathVariable("druggistId") Integer druggistId)
    {
        List<Integer> ids = druggistDoctorRelationDao.selectList(
                        new LambdaQueryWrapper<DruggistDoctorRelationEntity>()
                                .eq(DruggistDoctorRelationEntity::getDruggistId, druggistId)
                ).stream()
                .map(DruggistDoctorRelationEntity::getDoctorId)
                .collect(Collectors.toList());

        return Result.success(ids);
    }

    /**
     * 医生提交审核资料
     */
    @PostMapping("/submit")
    public Result<DoctorEntity> submit(@RequestBody DoctorEntity doctor)
    {
        return Result.success(doctorService.submit(doctor));
    }

    @GetMapping()
    public Result<?> list()
    {
        List<DoctorEntity> doctors = doctorService.listDoctors();
        return Result.success(doctors);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    public Result<DoctorEntity> info(@PathVariable("id") Integer id)
    {
		DoctorEntity doctor = doctorService.getById(id);

        return Result.success(doctor);
    }

    /**
     * 药商提交审核结果
     */
    @PostMapping("/verify")
    public Result<?> verify(@RequestBody DoctorEntity doctor)
    {
        if (VerifyStatus.PASSED.getValue() == doctor.getVerifyStatus()) {
            doctor.setMyInviteCode(Utils.genInviteCode());
        }
        doctorService.updateById(doctor);
        return Result.success();
    }
}
